<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;
use app\admin\controller\Calculate;
class Submit extends Common
{

    /**
     * 提交列表
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        if (request()->isAjax()) {

            if(session('aid')==1){
                $cate_list = '';
            }else{

                //查找今日可以编辑的考核项周期
                $cycle_data = Db::name('cycle')->where('status',1)->select();

//        拿到该教师的上级审核人 先判断该教师是否为审核人
                $section_id = Db::name('teacher')->where('id',session('aid'))->value('section_id');
                $check = Db::name('performance_check')->where('section_ids','like',"%\"{$section_id}\"%")->find();
                $check_name = Db::name('teacher')->where('id',$check['teacher_id'])->value('true_name');

                if($cycle_data) {
                    $str_cycle_ids = '';
                    foreach ($cycle_data as $k => $v) {

                        if ($v['id'] == 1) {

                            $today = (int)date("d", time());
                            if ((int)$v['start_time'] <= $today && (int)$v['end_time'] >= $today) {
                                $str_cycle_ids .= ','.$v['id'];
                            }
                        } elseif ($v['id'] == 2) {
                            $today = date("m-d", time());
                            if ($v['start_time'] && $v['end_time']) {
                                $start_time = unserialize($v['start_time']);
                                $end_time = unserialize($v['end_time']);
                                if ((($today >= $start_time[0]) && ($today <= $end_time[0])) || (($today >= $start_time[1]) && ($today <= $end_time[1]))) {
                                    $str_cycle_ids .= ','.$v['id'];
                                }
                            }
                        }elseif ($v['id'] == 3) {
                            $today = date("m-d",time());

                            if($v['start_time'] && $v['end_time'] && ($today>=$v['start_time'])&&($today<=$v['end_time'])){
                                $str_cycle_ids .= ','.$v['id'];
                            }
                        }
                    }


//                    拿到教师职务
                    $position_ids = Db::name('teacher')->where('id',session('aid'))->value('position_id');

                    if($position_ids){
                        $str_position_ids = implode(',',array_keys(json_decode($position_ids,true)));
                    }

//                    根据职务拿到考核项分类ids
                    $performance_cate_ids = Db::name('teacher_position')->field('performance_cate_ids')->where('position_id','in',$str_position_ids)->select();

                    $str_performance_cate_ids = '';
                    if($performance_cate_ids){
                        foreach ($performance_cate_ids as $k=>$v){
                            if($v['performance_cate_ids']){
                                $str_performance_cate_ids .= ','.implode(',',array_filter(array_unique(unserialize($v['performance_cate_ids']))));
                            }

                        }
                    }
                    $str_performance_cate_ids = implode(',',array_filter(array_unique(explode(',',$str_performance_cate_ids))));

//                    根据考核项分类ids和可编辑周期拿到可编辑考核项分类ids
                    $cate_list = Db::name('performance_cate')->where('cycle_id','in',$str_cycle_ids)->where('cate_id','in',$str_performance_cate_ids)->where('is_del',0)->field('cycle_id,cate_id,pid,title')->select();

                    if($cate_list){
                        foreach ($cate_list as $k=>$v){

                            $answer = Db::name('answer_record')->where('teacher_id',session('aid'))->where('performance_cate_id',$v['cate_id'])->find();
                            $cate_list[$k]['submit_status'] = '';
                            $cate_list[$k]['status'] = '';

                            if($answer){
                                $cate_list[$k]['submit_status'] = $answer['submit_status'];
                                if($answer['submit_status']==2){
                                    if($answer['second_status']==1&&$answer['status']==1){
                                        $cate_list[$k]['status'] = 1;
                                    }elseif ($answer['second_status']==0&&$answer['status']==0){
                                        $cate_list[$k]['status'] = 0;
                                    }else{
                                        $cate_list[$k]['status'] = 2;
                                    }
                                }

                            }
                        }

                    }
                }
            }
            return $result = ['code'=>0,'msg'=>'成功!','data'=>$cate_list,'count'=>count($cate_list),'check_name'=>'审核员：'.$check_name];

        }else{
            return $this->fetch('lists');
        }

    }

    /**
     * 提交
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function submit(){
        if (request()->isAjax()) {
            $cate_id = input('post.cate_id');
            Db::name('answer_record')->where('teacher_id',2)->where('performance_cate_id',$cate_id)->update(['submit_status'=>2]);
            $this->success('保存成功');
        }
    }

}